require(["../../conf/config.js"],function(){
	
	//导航条
	require(["jquery"],function(){
		//console.log($);
		//console.log($("#topNavs").children());
		
		//导航条划过特效
		$(".topNav ul li").hover(function(){ //hover需要两个参数
			//console.log($(this));
			//鼠标划过
			//console.log($(this).children().first())
			$(this).children().first().css({"color":"#395782"});
			$(this).children().removeClass("navHide").addClass("navActive");
			
		},function(){ //移出的函数
			$(this).children().first().css({"color":"#616161"});
			$(this).children().removeClass("navActive").addClass("navHide");
		})
		
	})
	
	//搜索框
	require(["animate"],function(animate){
		//搜索框显示
		let searchBtn = document.getElementsByClassName("topSearch")[0]; //获取搜索框的按钮
		let navLeft = document.getElementsByClassName("topNav")[0]; //获取头部坐标的导航
		let navRight = document.getElementsByClassName("topRight")[0]; //获取头部右边的导航
		let searchViewBtn = document.getElementsByClassName("topHeadSearch")[0]; //获取搜索框
		let closeSearch = document.getElementsByClassName("closebutton")[0]; //获取关闭搜索框
		let search = document.getElementById("search");
		
//		console.log(searchBtn);
//		console.log(navLeft);
//		console.log(navRight);
//		console.log(search);
		
		searchBtn.addEventListener("click",()=>{
			/*navLeft.className = "topNav navHide";
			navRight.className = "navRight navHide";
			searchViewBtn.className = "topHeadSearch navActive";*/
			animate(navLeft,{opacity:0});
			animate(navRight,{opacity:0},()=>{
				searchViewBtn.className = "topHeadSearch navActive";
			});
		})
		
		//关闭搜索框
		closeSearch.addEventListener("click",()=>{
			searchViewBtn.className = "topHeadSearch navHide";
			animate(navLeft,{opacity:10});
			animate(navRight,{opacity:10});
		})
		
		//jsonp获取数据
		search.addEventListener("input",()=>{
			//https://s.lenovo.com.cn/search/suggest?
			let _script = document.createElement("script");
			_script.src = "https://s.lenovo.com.cn/search/suggest?kw="+search.value+"&cb=searchItems"
			document.body.appendChild(_script);
			
		})
		
		let result = document.getElementById("searchResult"); //显示搜索结果
		
		window.searchItems = function(data){ //jsonp的回掉函数，读取数据
			result.innerHTML = "";
			data.normal.forEach(function(item){
				let _li = document.createElement("li");
				_li.innerText = item.word;
				result.appendChild(_li);
				console.log(item.word);
			})
			
		}
		
	})
	
	//banner轮播图
	require(["mySwiper"],function(Swiper){
		let imgBanner = new Swiper("#Oul",{left:true});	//给要播放的ul写一个id
	})
	
	//content1的商品轮播
	require(["jquery","contentItemTab","picSrc"],function($,contentItemTab,picSrc){
		//实现划过效果
		/*$(".itemTab").on("hover","li",function(){
			function(){
				$(".fontcontrol").css({"color":"#3A5883"});
				console.log("aa");
			},function(){
				$(".fontcontrol").css({"color":"#616161"});
			}
		})*/
		//console.log(picSrc)
		let content1 = document.getElementsByClassName("content1")[0]; //找到要实行划过效果的div
		let content2 = document.getElementsByClassName("content2")[0];
		let content3 = document.getElementsByClassName("content3")[0];
		//console.log(content1);
		//console.log(content2);
		contentItemTab(content1,0);  //引入了模块,后面的数字为第几个模块的下标
		//console.log("wwqqqq");
		contentItemTab(content2,1);
		contentItemTab(content3,2);
		
		//实现自动轮播效果
	})
	
	//热门推荐点击
	require(["jquery"],function($){
		let ulWidth = document.getElementById("hot");
		let w = parseInt(getComputedStyle(ulWidth).left);
		$(".hotLeft").click(function(){
			//console.log(ulWidth);
			//判断是否越界
			//console.log(w)
			if(w>=3707){
				ulWidth.style.left = 0;
			}else{
				ulWidth.style.left = w - 337.25 + "px";
			}
			//console.log(getComputedStyle(ulWidth).left)
			
		})
		$(".hotRight").click(function(){
			ulWidth.style.left = w + 337.25 + "px";
		})
	})
	
	
	//获取数据
	require(["pageData","jquery","picNum"],(function(ajax,$,itemNum){
		
		let content1 = document.getElementsByClassName("content1")[0];
		//itemNum是每组图片的编号
		ajax(itemNum.homeNum[0],0); //传不同内容块的编号
		ajax(itemNum.homeNum[1],1);
		ajax(itemNum.homeNum[2],2);
		
	}))
	
	//点击登录
	require(["jquery","loginMenu","userLogin","getCookie","setCookie"],function($,loginMenu,userLogin,getCookie,setCookie){
		//console.log(name,pwd)
		let isCookie = document.cookie.indexOf("username"); //查找cookie是否存在
		console.log(isCookie)
		
		//判断是否有用户登录
		if(isCookie != -1){
			//读取cookie
			let name = getCookie("username");
			let pwd = getCookie("userpass");
			//修改登录字符的样式
			$("#loginBtn").css("width","8em");
			$("#loginBtn").nextAll().hide();
			$("#loginBtn").text("lenovo"+name[1]);
			
			//滑过用户名
			$("#loginBtn").hover(function(){
				//$(".user-msg").removeClass("navHide");
				$(".user-msg").slideDown("slow");
				
			},function(){
				
				let time = setTimeout(function(){
					//$(".user-msg").addClass("navHide");
					$(".user-msg").hide();
				},500);
				
				//消息块的显示
				$(".user-msg").hover(function(){
					clearTimeout(time)
					$(".user-msg").removeClass("navHide");
				},function(){
					//$(".user-msg").addClass("navHide");
					$(".user-msg").hide();
				});
			})
			
			//退出登录
			$("#log-out").click(function(){
				//让cookie过期
				setCookie("username",name[1],-1);
				setCookie("userpass",pwd[1],-1);
				
				alert("退出成功");
				$("#loginBtn").css("width","2em");
				$("#loginBtn").nextAll().show();
				$("#loginBtn").text("登录");
			})
			
		}else{
			//如果未登录
			$("#loginBtn").css("width","2em");
			$("#loginBtn").nextAll().show();
			
			loginMenu(); //引入登录模板
			$("#loginBtn").click(function(){
				//alert()
				$(".login_shadow").css("display","block");
				$(".login-main").show();
			})
			$(".cancel-login").click(function(){
				$(".login-main").hide();
				$(".login_shadow").hide();
			})
			
			//点击登录按钮，进行用户身份判断
			userLogin();
		}
		
		
	})
	
	
	
	//购物车跳转和购物车商品的数量
	require(["jquery","getCookie"],function($,getCookie){
		
		//读取购物车的商品数量
		let items_count = JSON.parse(getCookie("shoppingCar")[1]).length;
		$("#buyCar-count").text(items_count);
		//console.log(items_count)
		
		//购物车点击
		$("#shoppingCar").click(function(){
			//console.log($(location))
//			$(location).attr('href','/shoppingCar');
			window.location.href="/pages/index/shoppingCar.html";
		})
		$("#add-user").click(function(){
			window.location.href="/pages/index/registered.html";
		})
	})
	
	
	require(["jquery",],function($){
		window.addEventListener("scroll",function(){
			//console.log( $(window).scrollTop() )
			if($(window).scrollTop()>=500){
				$(".stairs").show();
			}else{
				$(".stairs").hide();
			}
		})
		
		let stairs =Array.from($(".stairs").children().children());
		//let contens =Array.from($(".content1"));
		//console.log($($(".content1")[1]).offset().top)
		
		stairs.forEach(function(lis,index){
			lis.addEventListener("click",function(){
				//console.log($($(".content1")[index]).offset().top)
				if(index == 0){
					$("html").scrollTop(0)
				}else if(index == 4){
					$("html").scrollTop($($(".foot")[0]).offset().top);
				}else{	
					$("html").scrollTop($($(".content1")[index-1]).offset().top)
				}
			})
			
			lis.addEventListener("mouseenter",function(){
				if(index !=0 && index != 4){
					this.style.background = "#C00";
					this.style.color = "white"
				}
			})
			lis.addEventListener("mouseout",function(){
				if(index != 0 && index != 4){
					this.style.background = "white";
					this.style.color = "#C00"
				}
			})
		})
		
	})
})


